/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/8/29  10:46
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.view.cn.views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by baichuan on 2016/8/29.
 */
public class AntSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    public String TAG = "";

    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    private boolean isDraw = false;
    private Thread mThread;


    public AntSurfaceView(Context context) {
        super(context);
        init();
    }

    public AntSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AntSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init() {
        TAG = this.getClass().getSimpleName();
        mHolder = this.getHolder();
        mHolder.addCallback(this);
    }


    /***************************************************************/
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        isDraw = true;
        mThread = new Thread(this);
        mThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int wdith, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isDraw = false;
    }

    @Override
    public void run() {

        try {

            mCanvas = mHolder.lockCanvas();
            drawView(mCanvas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (mHolder != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void drawView(Canvas mCanvas) {

    }

}
